package com.carbon.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.common.api.Paging;
import com.carbon.common.service.BaseServiceImpl;
import com.carbon.domain.common.ApiResult;
import com.carbon.system.entity.*;
import com.carbon.system.mapper.MenuMapper;
import com.carbon.system.mapper.RoleMenuMapper;
import com.carbon.system.param.SysRoleMenuAddParam;
import com.carbon.system.param.SysRoleQueryParam;
import com.carbon.system.vo.SysAccountRoleVo;
import com.carbon.system.vo.SysRoleQueryVo;
import com.carbon.system.mapper.SysRoleMapper;
import com.carbon.system.mapper.SysRoleMenuMapper;
import com.carbon.system.service.SysAccountRoleService;
import com.carbon.system.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 角色  服务实现类
 * </p>
 *
 * @author Li Jun
 * @since 2021-06-18
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public IPage<SysRole> getRoleList(String page, Page<SysRole> param) {
        return sysRoleMapper.getRoleList(page, param);
    }

    @Override
    public List<MenuVO> getRolePermissions(Long roleId) {
        List<Long> roleIds = Collections.singletonList(roleId);
         List<Long> menuIds = roleMenuMapper.selectMenuIdsByRoleIds(roleIds);
        if (menuIds == null || menuIds.isEmpty()) {
            return null; // 无菜单权限，返回空列表
        }

        // 3. 查询菜单详情（只查 is_menu=1 且 hidden=0 的菜单）
        List<MenuVO> menus = menuMapper.selectMenusByIds(menuIds);

        // 4. 构建树形结构（根节点 parentId=0）
        return menus;
    }

    @Override
    public Boolean saveRolePermissions(Long roleId, List<Long> permissionIds) {
        roleMenuMapper.deleteByRoleId(roleId);
        return roleMenuMapper.saveRolePermissions(roleId, permissionIds);
    }

    @Override
    public boolean saveRole(SysRole role) {
        return sysRoleMapper.saveRole(role);
    }

    @Override
    public void removeRoleMenu(Long id) {
        sysRoleMapper.removeRoleMenu(id);
    }

    // 递归构建树形菜单
    public List<MenuVO> buildMenuTree(List<MenuVO> menus, Integer parentId) {
        return menus.stream()
                .filter(menu -> menu.getParentId().equals(parentId)) // 筛选父节点
                .map(menu -> {
                    // 递归查询子菜单
                    menu.setChildren(buildMenuTree(menus, menu.getId()));
                    return menu;
                })
                .sorted((m1, m2) -> m1.getSort().compareTo(m2.getSort())) // 按排序号排序
                .collect(Collectors.toList());
    }

}
